﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VIKI.BLL;
using VIKI.LINQ;

namespace VIKI.Web.Home.Login
{
    public partial class Login : System.Web.UI.Page
    {
        private static readonly UserBLL userBll = new UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.HttpMethod == "POST")
                {
                    string userName = Request["userName"].ToString();
                    string password = Request["password"].ToString();
                    this._UserLogin(userName, password, false);
                }
                else
                {
                    string action = Request.QueryString["action"] != null ? Request.QueryString["action"].ToString() : "default";
                    if (action == "loginout")
                    {
                        this._UserLoginOut();
                    }
                }
            }
            this.Master.FindControl("TopnavPanel").Visible = false;
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            this._UserLogin(UserName.Text, Password.Text, AutoLogin.Checked);
        }

        private void _UserLogin(string userName, string password, bool autoLogin)
        {
            userName = userName.Trim();
            password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Trim(), "MD5");

            if (userName != "" && password != "")
            {
                User user = new UserBLL().FindUserByName(userName);
                if (user != null)
                {
                    if (user.UserPass == password)
                    {
                        if (autoLogin)
                        {
                            HttpCookie cookies = new HttpCookie("VikiUser");
                            cookies["UserID"] = user.UserId.ToString();
                            cookies["UserName"] = user.UserName;
                            Response.Cookies.Add(cookies);
                            cookies.Expires = DateTime.Now.AddDays(1);
                        }
                        Session["UserID"] = user.UserId;
                        Session["UserName"] = user.UserName;

                        Response.Redirect(Request.UrlReferrer.ToString());
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('用户名或密码错误')</script>");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('用户名不存在')</script>");
                }
            }
        }

        private void _UserLoginOut()
        {
            Session.Clear();
            Response.Cookies["VikiUser"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect(Request.UrlReferrer.ToString()); 
        }
    }
}